package com.cloudcc.boot.enums;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.Getter;

@Getter
public enum SapTypeEnum {

    SUCCESS("S", "成功"),
    FAILURE("E", "失败");


    private final String code;       // 状态代码（E=失败；S=成功）

    private final String description; // 状态描述

    /**
     * 枚举构造函数
     *
     * @param code        状态代码
     * @param description 状态描述
     */
    SapTypeEnum(String code, String description) {
        this.code = code;
        this.description = description;
    }

    @JsonValue
    public String getCode() {
        return code;
    }

    /**
     * 根据状态代码查找对应的枚举值
     *
     * @param code 要查找的状态代码（不区分大小写）
     * @return 匹配的枚举
     * @throws IllegalArgumentException 如果找不到匹配的状态代码
     */
    @JsonCreator
    public static SapTypeEnum fromCode(String code) {
        if (code == null) return null;

        return switch (code.trim().toUpperCase()) {
            case "S", "Y", "YES", "TRUE" -> SUCCESS;
            case "E", "N", "NO", "FALSE" -> FAILURE;
            default -> throw new IllegalArgumentException("无效的值: " + code);
        };
    }

    /**
     * 根据状态描述查找对应的枚举值
     *
     * @param description 要查找的状态描述（不区分大小写）
     * @return 匹配的枚举
     * @throws IllegalArgumentException 如果找不到匹配的状态描述
     */
    public static SapTypeEnum fromDescription(String description) {
        for (SapTypeEnum status : SapTypeEnum.values()) {
            if (status.description.equalsIgnoreCase(description)) {
                return status;
            }
        }
        throw new IllegalArgumentException("无效的状态描述: " + description);
    }

    @Override
    public String toString() {
        return code + " - " + description;
    }

}

